package spider

import (
	"context"
	"log"
	"time"

	"github.com/chromedp/chromedp"
)

type Audio struct {
	Name string
	Url  string
}

type AudioGraber struct {
}

func NewAudioGraber() *AudioGraber {
	return &AudioGraber{}
}

func (g *AudioGraber) Grab(menu Menu) Audio {
	ctx, cancel := chromedp.NewContext(context.Background())
	defer cancel()

	var src string
	var ok bool

	err := chromedp.Run(ctx,
		chromedp.Navigate(prefix+menu.Addr),
		chromedp.Sleep(3*time.Second),
		chromedp.AttributeValue("#jp_audio_0", "src", &src, &ok),
	)

	if err != nil {
		log.Fatalf("chromedp err: %v", err)
	}

	return Audio{
		Name: menu.Name,
		Url:  src,
	}
}
